﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Utils.Log;

namespace FrameworkTemplate.Windows.Controls
{
    /// <summary>
    /// LogControl.xaml 的交互逻辑
    /// </summary>
    public partial class LogControl : UserControl
    {
        int max_log_length = 200000;
        Data data;
        LogWorker logWorker;
        public LogControl()
        {
            data = new Data();
            InitializeComponent();
            log_textbox.SetBinding(TextBox.DataContextProperty, new Binding { Source = data, Path = new PropertyPath("Log"), Mode = BindingMode.OneWay });
            log_textbox.DataContextChanged += Log_textbox_DataContextChanged;
        }
        public void BindLogWorker(LogWorker logWorker)
        {
            this.logWorker = logWorker;
            logWorker.OnLog += LogWorker_OnLog;
        }

        private void LogWorker_OnLog(string log)
        {
            data.Log += log;
        }

        private void Log_textbox_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

            if (log_textbox.Text.Length >= max_log_length)
                log_textbox.Clear();
            log_textbox.AppendText((string)e.NewValue);
            log_textbox.ScrollToEnd();
        }
        

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            log_textbox.Clear();
        }

        private void Open_Click(object sender, RoutedEventArgs e) => System.Diagnostics.Process.Start(Log.LogPath());

        private void Log_textbox_Unloaded(object sender, RoutedEventArgs e)
        {
            if (logWorker != null)
                logWorker.OnLog -= LogWorker_OnLog;

        }
    }

    class Data : INotifyPropertyChanged
    {
        string log = "";

        public string Log
        {
            get
            {
                var t = log;
                log = "";
                return t;
            }
            set
            {
                log = value;
                //Logs.Add( log);
                NotifyPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        // This method is called by the Set accessor of each property.
        // The CallerMemberName attribute that is applied to the optional propertyName
        // parameter causes the property name of the caller to be substituted as an argument.
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
